/* Copyright (C) Abu Rizal, 2009.
 * 
 * This file is part of QurText.
 * 
 * QurText is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * QurText is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with QurText. If not, see <http://www.gnu.org/licenses/>.
 */
package qurtext.factory;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.NoSuchElementException;

import javax.jdo.PersistenceManager;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;

import org.xml.sax.InputSource;

import qurtext.domain.Chapter;
import qurtext.domain.Section;

public class ChapterFactory {

	private String getTanzilChapterName(int chapterNo) {
		String location = "/quran/suras/sura[@index='"+chapterNo+"']/@name";
		return getTanzilChapterMetadaa(location);
	}

	private String getTanzilChapterTransliteration(int chapterNo) {
		String location = "/quran/suras/sura[@index='"+chapterNo+"']/@tname";
		return getTanzilChapterMetadaa(location);
	}

	private String getTanzilChapterMetadaa(String location) {
		File xmlDocument = 
		    new File("WEB-INF/quran-data.xml");
		XPathFactory factory = new org.apache.xpath.jaxp.XPathFactoryImpl();
		XPath xPath=factory.newXPath();
		try {
			XPathExpression  xPathExpression=
			    xPath.compile(location);
			InputSource inputSource = 
			    new InputSource(new
			         FileInputStream(xmlDocument));
			return xPathExpression.evaluate(inputSource);
		} catch (XPathExpressionException e) {
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		return null;
	}

	@SuppressWarnings("unchecked")
	public List<Chapter> getAllChapterList() {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		pm.setDetachAllOnCommit(true);
		try {
			String query = "select from " + Chapter.class.getName();
			List<Chapter> resultList = new ArrayList<Chapter>();
			resultList.addAll((Collection<Chapter>) pm.newQuery(query)
					.execute());
//			for (Chapter chapter:resultList) {
//				StringBuffer title = new StringBuffer();
//				for (char c:chapter.getTitle().toCharArray()) {
//					String hexString = Integer.toHexString(c);
//					title.append("\\u" + "0000".substring(hexString.length()) + hexString);
//				}
//				System.out.println("{\"" + title + "\",\"" + chapter.getTransliteration() + "\"},");
//			}
			return resultList;
		} finally {
			pm.close();
		}
	}

	@SuppressWarnings("unchecked")
	private Chapter getChapter(PersistenceManager pm, int chapterNo) {
		try {
			String query = "select from " + Chapter.class.getName() + " where chapterNo==" + chapterNo;
			Chapter result=((Collection<Chapter>) pm.newQuery(query)
					.execute()).iterator().next();
			return result;
		} catch (NoSuchElementException e) {
			return null;
		}
	}

	private HashMap<Integer,Chapter> initialChapterMap= new HashMap<Integer,Chapter>();

	//TODO:get description for 558 rukus http://www.scribd.com/doc/16691610/Surah-Contents-an-Index-of-Quran
	public int initChapterList(int start) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
			if (start<=0) {
				String query = "select from " + Chapter.class.getName();
				pm.newQuery(query).deletePersistentAll();
			}
			if (initialChapterMap.size()<=0) {
				   int [] chapterSize={0,7,286,200,176,120,165,206,75,129,109,
							123,111,43,52,99,128,111,110,98,135,
							112,78,118,64,77,227,93,88,69,60,
							34,30,73,54,45,83,182,88,75,85,
							54,53,89,59,37,35,38,29,18,45,
							60,49,62,55,78,96,29,22,24,13,
							14,11,11,18,12,12,30,52,52,44,
							28,28,20,56,40,31,50,40,46,42,
							29,19,36,25,22,17,19,26,30,20,
							15,21,11,8,8,19,5,8,8,11,
							11,8,3,9,5,4,7,3,6,3,5,4,5,6};
				   int [][] sections  = {{1,1,7}
				   ,{2,1,7}
				   ,{2,8,20}
				   ,{2,21,29}
				   ,{2,30,39}
				   ,{2,40,46}
				   ,{2,47,59}
				   ,{2,60,61}
				   ,{2,62,71}
				   ,{2,72,82}
				   ,{2,83,86}
				   ,{2,87,96}
				   ,{2,97,103}
				   ,{2,104,112}
				   ,{2,113,121}
				   ,{2,122,129}
				   ,{2,130,141}
				   ,{2,142,147}
				   ,{2,148,152}
				   ,{2,153,163}
				   ,{2,164,167}
				   ,{2,168,176}
				   ,{2,177,182}
				   ,{2,183,188}
				   ,{2,189,196}
				   ,{2,197,210}
				   ,{2,211,216}
				   ,{2,217,221}
				   ,{2,222,228}
				   ,{2,229,231}
				   ,{2,232,235}
				   ,{2,236,242}
				   ,{2,243,248}
				   ,{2,249,253}
				   ,{2,254,257}
				   ,{2,258,260}
				   ,{2,261,266}
				   ,{2,267,273}
				   ,{2,274,281}
				   ,{2,282,283}
				   ,{2,284,286}
				   ,{3,1,9}
				   ,{3,10,20}
				   ,{3,21,30}
				   ,{3,31,41}
				   ,{3,42,54}
				   ,{3,55,63}
				   ,{3,64,71}
				   ,{3,72,80}
				   ,{3,81,91}
				   ,{3,92,101}
				   ,{3,102,109}
				   ,{3,110,120}
				   ,{3,121,129}
				   ,{3,130,143}
				   ,{3,144,148}
				   ,{3,149,155}
				   ,{3,156,171}
				   ,{3,172,180}
				   ,{3,181,189}
				   ,{3,190,200}
				   ,{4,1,10}
				   ,{4,11,14}
				   ,{4,15,22}
				   ,{4,23,25}
				   ,{4,26,33}
				   ,{4,34,42}
				   ,{4,43,50}
				   ,{4,51,59}
				   ,{4,60,70}
				   ,{4,71,76}
				   ,{4,77,87}
				   ,{4,88,91}
				   ,{4,92,96}
				   ,{4,97,100}
				   ,{4,101,104}
				   ,{4,105,112}
				   ,{4,113,115}
				   ,{4,116,126}
				   ,{4,127,134}
				   ,{4,135,141}
				   ,{4,142,152}
				   ,{4,153,162}
				   ,{4,163,171}
				   ,{4,172,176}
				   ,{5,1,5}
				   ,{5,6,11}
				   ,{5,12,19}
				   ,{5,20,26}
				   ,{5,27,34}
				   ,{5,35,43}
				   ,{5,44,50}
				   ,{5,51,56}
				   ,{5,57,66}
				   ,{5,67,77}
				   ,{5,78,86}
				   ,{5,87,93}
				   ,{5,94,100}
				   ,{5,101,108}
				   ,{5,109,115}
				   ,{5,116,120}
				   ,{6,1,10}
				   ,{6,11,20}
				   ,{6,21,30}
				   ,{6,31,41}
				   ,{6,42,50}
				   ,{6,51,55}
				   ,{6,56,60}
				   ,{6,61,70}
				   ,{6,71,82}
				   ,{6,83,90}
				   ,{6,91,94}
				   ,{6,95,100}
				   ,{6,101,110}
				   ,{6,111,121}
				   ,{6,122,129}
				   ,{6,130,140}
				   ,{6,141,144}
				   ,{6,145,150}
				   ,{6,151,154}
				   ,{6,155,165}
				   ,{7,1,10}
				   ,{7,11,25}
				   ,{7,26,31}
				   ,{7,32,39}
				   ,{7,40,47}
				   ,{7,48,53}
				   ,{7,54,58}
				   ,{7,59,64}
				   ,{7,65,72}
				   ,{7,73,84}
				   ,{7,85,93}
				   ,{7,94,99}
				   ,{7,100,108}
				   ,{7,109,126}
				   ,{7,127,129}
				   ,{7,130,141}
				   ,{7,142,147}
				   ,{7,148,151}
				   ,{7,152,157}
				   ,{7,158,162}
				   ,{7,163,171}
				   ,{7,172,181}
				   ,{7,182,188}
				   ,{7,189,206}
				   ,{8,1,10}
				   ,{8,11,19}
				   ,{8,20,28}
				   ,{8,29,37}
				   ,{8,38,44}
				   ,{8,45,48}
				   ,{8,49,58}
				   ,{8,59,64}
				   ,{8,65,69}
				   ,{8,70,75}
				   ,{9,1,6}
				   ,{9,7,16}
				   ,{9,17,24}
				   ,{9,25,29}
				   ,{9,30,37}
				   ,{9,38,42}
				   ,{9,43,59}
				   ,{9,60,66}
				   ,{9,67,72}
				   ,{9,73,80}
				   ,{9,81,89}
				   ,{9,90,99}
				   ,{9,100,110}
				   ,{9,111,118}
				   ,{9,119,122}
				   ,{9,123,129}
				   ,{10,1,10}
				   ,{10,11,20}
				   ,{10,21,30}
				   ,{10,31,40}
				   ,{10,41,53}
				   ,{10,54,60}
				   ,{10,61,70}
				   ,{10,71,82}
				   ,{10,83,92}
				   ,{10,93,103}
				   ,{10,104,109}
				   ,{11,1,8}
				   ,{11,9,24}
				   ,{11,25,35}
				   ,{11,36,49}
				   ,{11,50,60}
				   ,{11,61,68}
				   ,{11,69,83}
				   ,{11,84,95}
				   ,{11,96,109}
				   ,{11,110,123}
				   ,{12,1,6}
				   ,{12,7,20}
				   ,{12,21,29}
				   ,{12,30,35}
				   ,{12,36,42}
				   ,{12,43,49}
				   ,{12,50,57}
				   ,{12,58,68}
				   ,{12,69,79}
				   ,{12,80,93}
				   ,{12,94,104}
				   ,{12,105,111}
				   ,{13,1,7}
				   ,{13,8,18}
				   ,{13,19,26}
				   ,{13,27,31}
				   ,{13,32,37}
				   ,{13,38,43}
				   ,{14,1,6}
				   ,{14,7,12}
				   ,{14,13,21}
				   ,{14,22,27}
				   ,{14,28,34}
				   ,{14,35,41}
				   ,{14,42,52}
				   ,{15,1,15}
				   ,{15,16,25}
				   ,{15,26,44}
				   ,{15,45,60}
				   ,{15,61,79}
				   ,{15,80,99}
				   ,{16,1,9}
				   ,{16,10,21}
				   ,{16,22,25}
				   ,{16,26,34}
				   ,{16,35,40}
				   ,{16,41,50}
				   ,{16,51,60}
				   ,{16,61,65}
				   ,{16,66,70}
				   ,{16,71,76}
				   ,{16,77,83}
				   ,{16,84,89}
				   ,{16,90,100}
				   ,{16,101,110}
				   ,{16,111,119}
				   ,{16,120,128}
				   ,{17,1,10}
				   ,{17,11,22}
				   ,{17,23,30}
				   ,{17,31,40}
				   ,{17,41,52}
				   ,{17,53,60}
				   ,{17,61,70}
				   ,{17,71,77}
				   ,{17,78,84}
				   ,{17,85,93}
				   ,{17,94,100}
				   ,{17,101,111}
				   ,{18,1,12}
				   ,{18,13,17}
				   ,{18,18,22}
				   ,{18,23,31}
				   ,{18,32,44}
				   ,{18,45,49}
				   ,{18,50,53}
				   ,{18,54,59}
				   ,{18,60,70}
				   ,{18,71,82}
				   ,{18,83,101}
				   ,{18,102,110}
				   ,{19,1,15}
				   ,{19,16,40}
				   ,{19,41,50}
				   ,{19,51,65}
				   ,{19,66,82}
				   ,{19,83,98}
				   ,{20,1,24}
				   ,{20,25,54}
				   ,{20,55,76}
				   ,{20,77,89}
				   ,{20,90,104}
				   ,{20,105,115}
				   ,{20,116,128}
				   ,{20,129,135}
				   ,{21,1,10}
				   ,{21,11,29}
				   ,{21,30,41}
				   ,{21,42,50}
				   ,{21,51,75}
				   ,{21,76,93}
				   ,{21,94,112}
				   ,{22,1,10}
				   ,{22,11,22}
				   ,{22,23,25}
				   ,{22,26,33}
				   ,{22,34,38}
				   ,{22,39,48}
				   ,{22,49,57}
				   ,{22,58,64}
				   ,{22,65,72}
				   ,{22,73,78}
				   ,{23,1,22}
				   ,{23,23,32}
				   ,{23,33,50}
				   ,{23,51,77}
				   ,{23,78,92}
				   ,{23,93,118}
				   ,{24,1,10}
				   ,{24,11,20}
				   ,{24,21,26}
				   ,{24,27,34}
				   ,{24,35,40}
				   ,{24,41,50}
				   ,{24,51,57}
				   ,{24,58,61}
				   ,{24,62,64}
				   ,{25,1,9}
				   ,{25,10,20}
				   ,{25,21,34}
				   ,{25,35,44}
				   ,{25,45,60}
				   ,{25,61,77}
				   ,{26,1,9}
				   ,{26,10,33}
				   ,{26,34,52}
				   ,{26,53,69}
				   ,{26,70,104}
				   ,{26,105,122}
				   ,{26,123,140}
				   ,{26,141,159}
				   ,{26,160,175}
				   ,{26,176,191}
				   ,{26,192,227}
				   ,{27,1,14}
				   ,{27,15,31}
				   ,{27,32,44}
				   ,{27,45,58}
				   ,{27,59,66}
				   ,{27,67,82}
				   ,{27,83,93}
				   ,{28,1,13}
				   ,{28,14,21}
				   ,{28,22,28}
				   ,{28,29,42}
				   ,{28,43,50}
				   ,{28,51,60}
				   ,{28,61,75}
				   ,{28,76,82}
				   ,{28,83,88}
				   ,{29,1,13}
				   ,{29,14,22}
				   ,{29,23,30}
				   ,{29,31,44}
				   ,{29,45,51}
				   ,{29,52,63}
				   ,{29,64,69}
				   ,{30,1,10}
				   ,{30,11,19}
				   ,{30,20,27}
				   ,{30,28,40}
				   ,{30,41,53}
				   ,{30,54,60}
				   ,{31,1,11}
				   ,{31,12,19}
				   ,{31,20,30}
				   ,{31,31,34}
				   ,{32,1,11}
				   ,{32,12,22}
				   ,{32,23,30}
				   ,{33,1,8}
				   ,{33,9,20}
				   ,{33,21,27}
				   ,{33,28,34}
				   ,{33,35,40}
				   ,{33,41,52}
				   ,{33,53,58}
				   ,{33,59,68}
				   ,{33,69,73}
				   ,{34,1,9}
				   ,{34,10,21}
				   ,{34,22,30}
				   ,{34,31,36}
				   ,{34,37,45}
				   ,{34,46,54}
				   ,{35,1,7}
				   ,{35,8,14}
				   ,{35,15,26}
				   ,{35,27,37}
				   ,{35,38,45}
				   ,{36,1,12}
				   ,{36,13,32}
				   ,{36,33,50}
				   ,{36,51,67}
				   ,{36,68,83}
				   ,{37,1,21}
				   ,{37,22,74}
				   ,{37,75,113}
				   ,{37,114,138}
				   ,{37,139,182}
				   ,{38,1,14}
				   ,{38,15,26}
				   ,{38,27,40}
				   ,{38,41,64}
				   ,{38,65,88}
				   ,{39,1,9}
				   ,{39,10,21}
				   ,{39,22,31}
				   ,{39,32,41}
				   ,{39,42,52}
				   ,{39,53,63}
				   ,{39,64,70}
				   ,{39,71,75}
				   ,{40,1,9}
				   ,{40,10,20}
				   ,{40,21,27}
				   ,{40,28,37}
				   ,{40,38,50}
				   ,{40,51,60}
				   ,{40,61,68}
				   ,{40,69,78}
				   ,{40,79,85}
				   ,{41,1,8}
				   ,{41,9,18}
				   ,{41,19,25}
				   ,{41,26,32}
				   ,{41,33,44}
				   ,{41,45,54}
				   ,{42,1,9}
				   ,{42,10,19}
				   ,{42,20,29}
				   ,{42,30,43}
				   ,{42,44,53}
				   ,{43,1,15}
				   ,{43,16,25}
				   ,{43,26,35}
				   ,{43,36,45}
				   ,{43,46,56}
				   ,{43,57,67}
				   ,{43,68,89}
				   ,{44,1,29}
				   ,{44,30,42}
				   ,{44,43,59}
				   ,{45,1,11}
				   ,{45,12,21}
				   ,{45,22,26}
				   ,{45,27,37}
				   ,{46,1,10}
				   ,{46,11,20}
				   ,{46,21,26}
				   ,{46,27,35}
				   ,{47,1,11}
				   ,{47,12,19}
				   ,{47,20,28}
				   ,{47,29,38}
				   ,{48,1,10}
				   ,{48,11,17}
				   ,{48,18,26}
				   ,{48,27,29}
				   ,{49,1,10}
				   ,{49,11,18}
				   ,{50,1,15}
				   ,{50,16,29}
				   ,{50,30,45}
				   ,{51,1,23}
				   ,{51,24,46}
				   ,{51,47,60}
				   ,{52,1,28}
				   ,{52,29,49}
				   ,{53,1,25}
				   ,{53,26,32}
				   ,{53,33,62}
				   ,{54,1,22}
				   ,{54,23,40}
				   ,{54,41,55}
				   ,{55,1,25}
				   ,{55,26,45}
				   ,{55,46,78}
				   ,{56,1,38}
				   ,{56,39,74}
				   ,{56,75,96}
				   ,{57,1,10}
				   ,{57,11,19}
				   ,{57,20,25}
				   ,{57,26,29}
				   ,{58,1,6}
				   ,{58,7,13}
				   ,{58,14,22}
				   ,{59,1,10}
				   ,{59,11,17}
				   ,{59,18,24}
				   ,{60,1,6}
				   ,{60,7,13}
				   ,{61,1,9}
				   ,{61,10,14}
				   ,{62,1,8}
				   ,{62,9,11}
				   ,{63,1,8}
				   ,{63,9,11}
				   ,{64,1,10}
				   ,{64,11,18}
				   ,{65,1,7}
				   ,{65,8,12}
				   ,{66,1,7}
				   ,{66,8,12}
				   ,{67,1,14}
				   ,{67,15,30}
				   ,{68,1,33}
				   ,{68,35,52}
				   ,{69,1,37}
				   ,{69,38,52}
				   ,{70,1,35}
				   ,{70,36,44}
				   ,{71,1,20}
				   ,{71,21,28}
				   ,{72,1,19}
				   ,{72,20,28}
				   ,{73,1,19}
				   ,{73,20,20}
				   ,{74,1,31}
				   ,{74,32,56}
				   ,{75,1,30}
				   ,{75,31,40}
				   ,{76,1,22}
				   ,{76,23,31}
				   ,{77,1,40}
				   ,{77,41,50}
				   ,{78,1,30}
				   ,{78,31,40}
				   ,{79,1,26}
				   ,{79,27,46}
				   ,{80,1,42}
				   ,{81,1,29}
				   ,{82,1,19}
				   ,{83,1,36}
				   ,{84,1,25}
				   ,{85,1,22}
				   ,{86,1,17}
				   ,{87,1,19}
				   ,{88,1,26}
				   ,{89,1,30}
				   ,{90,1,20}
				   ,{91,1,15}
				   ,{92,1,21}
				   ,{93,1,11}
				   ,{94,1,8}
				   ,{95,1,8}
				   ,{96,1,19}
				   ,{97,1,5}
				   ,{98,1,8}
				   ,{99,1,8}
				   ,{100,1,11}
				   ,{101,1,11}
				   ,{102,1,8}
				   ,{103,1,3}
				   ,{104,1,9}
				   ,{105,1,5}
				   ,{106,1,4}
				   ,{107,1,7}
				   ,{108,1,3}
				   ,{109,1,6}
				   ,{110,1,3}
				   ,{111,1,5}
				   ,{112,1,4}
				   ,{113,1,5}
				   ,{114,1,6}};
				for (int i=1;i<chapterSize.length;i++) {
					Chapter chapter=getChapter(pm, i);
					if (null==chapter) {
						chapter=new Chapter();
					} 
					chapter.setChapterNo(i);
					chapter.setStartVerse(1);
					chapter.setEndVerse(chapterSize[i]);
					chapter.setTitel(getTanzilChapterName(i));
					chapter.setTransliteration(getTanzilChapterTransliteration(i));
					pm.makePersistent(chapter);
					initialChapterMap.put(i,chapter);
				}
				for (int i=0;i<sections.length;i++) {
					int[] sectionData=sections[i];
					Chapter chapter=initialChapterMap.get(sectionData[0]);
					Section section = chapter.getSection(sectionData[1]);
					if (null==section) {
						section=new Section();
						chapter.addSection(section);
					}
					section.setSectionNo(i);
					section.setChapterNo(chapter.getChapterNo());
					section.setStartVerse(sectionData[1]);
					section.setEndVerse(sectionData[2]);
					pm.makePersistent(section);
				}
			}
			Chapter chapter=initialChapterMap.get(start);
			if (null==chapter)
				return -1;
		} finally {
			pm.close();
		}
		return start+1;
	}

}
